// pages/goods-specs/index.js
import {
    ReDistributionMall
} from "../../api/shipping.js"
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        btnText: {
            type: String,
            value: ''
        },
        dataObj: {
            type: Object,
            value: {}
        },
        // 规格商品列
        option: {
            type: Array,
            value: []
        },
        disabled: {
            type: Boolean,
            value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        isShow: false,
        activeChild: 0,
        goodsList: [{
            goodsImg: "/images/element_christmas_xpzq@2x.png",
            goodsName: "商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称",
            goodsPrice: "310.00",
            goodsSpecs: "规格规格规格个规格规格规格规格规格规格个规格规格规格",
            goodsSpecsCount: 2
        }, ],
        specsList: ['2.5kg', '3.5kg'],
        active: 0, //当前选中规格
        specsName: "",
        propeprice:'',
        count: 1,
        thumb: '',
        property_price:'',
        propertydata:{}
    },

    observers: {
        
        'show': function (newVal, oldVal) {
            console.log('==',this.data.dataObj)

            let count = 1
            // if(this.data.dataObj) {
            //     if (this.data.dataObj.property !== undefined && this.data.dataObj.property[this.data.active].child.length > 0) {
            //         count = this.data.dataObj.property[this.data.active].child[this.data.activeChild].number
            //     }else{
            //         count = this.data.dataObj.property[this.data.active].number
            //     }
            //     this.setData({
            //         count: count
            //     })
            // }
           
             if (this.data.dataObj && this.data.dataObj.detailType == 7) {
                let dataObj = this.data.dataObj
                this.data.thumb = dataObj.thumb.replace('http://static.chonglaoban.cn', '')
                this.data.property_price = dataObj.price
                dataObj.name = dataObj.goodsname
                dataObj.property_name = dataObj.propertyname

                if (data.child[this.data.activeChild].inventory == 0) {

                    this.setData({
    
                        disabled: true,
                        btnText: "已售罄"
                    })
                } else {
    
                    this.setData({
    
                        disabled: false,
                        btnText: "加入购物车"
                    })
                }
                this.setData({
                    property_price: this.data.property_price,
                    thumb: this.data.thumb,
                    dataObj: dataObj
                })
             }else{
                 if (newVal && this.data.dataObj.type == 0) { // 普通
                     this.printImageInfo();
                     if(this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                         let data = this.data.dataObj.property[this.data.active]
                         if (data.child[this.data.activeChild].inventory == 0) {
                             this.setData({
                                 disabled: true,
                                 btnText: "已售罄"
                             })
                         }else{
                            
                         }
                         let obj = {
                             id :this.data.dataObj.id,
                             propertyid : this.data.propertydata
                         }
                         this.data.dataObj.propertyid = data.child[this.data.activeChild].id
                         this.data.dataObj.propertyname = data.property_name
                         this.data.dataObj.thumb = data.child[this.data.activeChild].img
                         console.log(this.data.dataObj.thumb)
                         this.setData({
                            thumb: this.data.dataObj.thumb
                         })
                     }else {
                         this.data.propertydata = {productid: this.data.dataObj.propertyid}
                         let obj = {
                             id :this.data.dataObj.id,
                             propertyid : this.data.propertydata
                         }
                     }
                 } else if (newVal && this.data.dataObj.type == 1) {//一件代发
                     if(this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                         this.data.propertydata = {productid:this.data.dataObj.property[this.data.active].child[this.data.activeChild].id}
                         let obj = {
                             id :this.data.dataObj.id,
                             propertyid : this.data.propertydata
                         }
                         this.getOneGoodsLists(obj)
                     }else {
                         this.data.propertydata = {productid: this.data.dataObj.propertyid}
                         let obj = {
                             id :this.data.dataObj.id,
                             propertyid : this.data.propertydata
                         }
                         this.getOneGoodsLists(obj)
                     }
                 }else if (newVal && this.data.dataObj.type == 2) {//京东
                     this.data.propertydata = {id:this.data.dataObj.property[this.data.active].child[this.data.activeChild].id}
                 }
             }
             
            this.setData({isShow:newVal})
        },
        'dataObj': function (newVal, oldVal) {
            console.log('接收到的对象：', newVal,newVal.property[0].child[0].spec);
            // this.data.dataObj.property.forEach(element => {
            //     element.child.forEach(elementj => {
            //         Object.assign(elementj
            //             ,{
            //                 property_price: elementj.spec
            //             })
            //     });
            // });
          
            // this.setData({
              
            //     active: 0,
            //     activeChild: 0, // 重置第二个规格栏的选中状态
            //     thumb: newVal.property[0].child[0].img,
            //     property_price:newVal.property[0].child[0].spec,
            //     propeprice:newVal.property[0].child[0].price,
            //     count: newVal.property[0].child[0].number,
            // });
        },
       
    },
    /**
     * 组件的方法列表
     */
    methods: {
        printImageInfo() {
            let thumbimg = this.data.dataObj.thumb
            console.log(thumbimg)
            if (thumbimg.startsWith("http")) {

                thumbimg = thumbimg.replace("http://static.chonglaoban.cn", "")
            }
            this.setData({
                thumb: thumbimg,
                property_price: this.data.dataObj.real_price
            });
        },
       
        //获取一件代发购物车商品数量
        getOneGoodsLists(item) {
            ReDistributionMall({
                act: 'oneGoodsInfo',
                id :item.id ,
                propertyid : item.propertyid.productid 
        
            }).then(res => {
                if(res.cart_numbers == '0') {
                    res.cart_numbers = 1
                }
                wx.nextTick(() => {
                    this.setData({
                        count: res.cart_numbers || 1
                    })
                })
            });
        },


        /**
         * 选择规格回调函数方法
         * @param {*} e 
         */
        // handleSelSpecs(e) {
        //     console.log("diyiggeguige", e.currentTarget.dataset)
        //     const {
        //         index,
        //         value
        //     } = e.currentTarget.dataset
        //     this.setData({
        //         active: index,
        //         specsName: value
        //     })
        // },
        handleSelSpecs(e) {
            console.log('切换',e)
            console.log(this.data.dataObj,this.data.activeChild)

            const index = e.currentTarget.dataset.index;
            this.data.dataObj.propertyid = this.data.dataObj.property[index].child[this.data.activeChild].id
            this.data.dataObj.propertyname = e.currentTarget.dataset.value.property_name
            let data = this.data.dataObj.property[index]
                    if (data.child[this.data.activeChild].inventory == 0) {
                        this.setData({
                            disabled: true,
                            btnText: "已售罄"
                        })
                    }else {
                        this.setData({
                            disabled: false,
                            btnText: "加入购物车"
                        })
                    }
            this.setData({
                active: index,
                activeChild: 0, // 重置第二个规格栏的选中状态
                thumb: this.data.dataObj.property[index].child[this.data.activeChild].img,
                property_price:this.data.dataObj.property[index].child[this.data.activeChild].spec,
                count: this.data.dataObj.property[index].child[this.data.activeChild].number,
            });
        },
        handleSelSpecsChild(e) {
            const index = e.currentTarget.dataset.index;
            this.setData({
                activeChild: index,
                property_price:this.data.dataObj.property[this.data.active].child[index].price,
                count:this.data.dataObj.property[this.data.active].child[index].number,
              
            });
            this.data.propertydata = {...e.currentTarget.dataset.value}
        },
        /**
         * 商品数量变化回调函数方法
         * @param {*} e 
         */
        handleNumChange(e) {
            this.setData({
                count: e.detail
            })
        },
        /**
         * 确认按钮回调函数方法
         */
        onConfirm() {
            let num 
            // tips = 0商品列表 tips = 1 一件代发 
            if(this.data.dataObj.type == 1) {
                num = 1
            }else {
                num = 0
            }
            console.log("提交的data0",this.data)
            this.triggerEvent('confirm', {
                index: this.data.active,
                name: this.data.specsList[this.data.active],
                data: this.data.dataObj,
                count: this.data.count || 1,
                type: num,
                commodityId:this.data.dataObj.id
            })
        },
        /**
         * 关闭
         */
        close() {
            this.triggerEvent('close')
        }
    }
})